﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using PurchaseOrderExtra;

namespace PresentationLayer
{
    public partial class ApprovePurchaseOrderList : System.Web.UI.Page
    {
        ApprovePurchaseOrderControl control;

        string supplierId;
        string supplierName;
        int requestedBy;
        string requestedByString;


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 6 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                control = new ApprovePurchaseOrderControl();

                if (!IsPostBack)
                {
                    // Store SupplierID in Session
                    supplierId = "ALL";
                    supplierName = "All Supplier";
                    requestedBy = 0;
                    requestedByString = requestedBy.ToString();
                    List<string> searchInfo = new List<string>();
                    searchInfo.Add(supplierId);
                    searchInfo.Add(supplierName);
                    searchInfo.Add(requestedByString);
                    ViewState["ApprovePO"] = searchInfo;

                    // Populate 2 dropdownlist
                    PopulateSupplierName();
                    PopulateRequestedBy();
                    GenerateGridView();
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            try
            {
                supplierId = ddlSupplierName.SelectedValue;
                supplierName = ddlSupplierName.SelectedItem.Text;
                requestedBy = Convert.ToInt32(ddlRequestedBy.SelectedValue);
                requestedByString = requestedBy.ToString();
                List<string> searchInfo = new List<string>();
                searchInfo.Add(supplierId);
                searchInfo.Add(supplierName);
                searchInfo.Add(requestedByString);
                // Update SUpplierID in Session
                ViewState["ApprovePO"] = searchInfo;

                if (supplierId.Equals("ALL"))
                {
                    if (requestedBy == 0)
                    {
                        GenerateGridView();
                    }
                    else
                    {
                        GenerateGridViewByRequestedBy();
                    }
                }
                else
                {
                    if (requestedBy == 0)
                    {
                        GenerateGridViewBySupplierId();
                    }
                    else
                    {
                        GenerateGridViewBySupplierIdAndRequestedBy();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void lbtnViewDetailGV_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
                Label lblPOId = (Label)clickedRow.FindControl("lblPOIdGV");
                string poIdString = lblPOId.Text;

                // Save to HttpSession
                Session["PODetail"] = poIdString;

                // Redirect to view details page
                string redirectURL = control.ViewPurchaseOrderDetail();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

        #region Populate Dropdownlist and generate GridView
        private void PopulateSupplierName()
        {
            List<Supplier> list = control.DisplayAllSupplier();
            ddlSupplierName.DataSource = list;
            ddlSupplierName.DataTextField = "SupplierName";
            ddlSupplierName.DataValueField = "SupplierID";
            ddlSupplierName.DataBind();
        }

        private void PopulateRequestedBy()
        {
            List<Employee> list = control.DisplayAllStoreClerk();
            ddlRequestedBy.DataSource = list;
            ddlRequestedBy.DataTextField = "EmployeeName";
            ddlRequestedBy.DataValueField = "EmployeeID";
            ddlRequestedBy.DataBind();
        }

        private void GenerateGridView()
        {
            lblMessage.Text = "";
            List<PurchaseOrderListDisplay> list = control.GetAllPurchaseOrder();
            if (list.Count <= 0)
            {
                lblMessage.Text = "There is no purchase order requested by " + ddlRequestedBy.SelectedItem.Text + " for " + ddlSupplierName.SelectedItem.Text;
            }
            gvPurchaseOrderList.DataSource = list;
            gvPurchaseOrderList.DataBind();
        }

        private void GenerateGridViewBySupplierId()
        {
            lblMessage.Text = "";
            List<string> searchInfo = ViewState["ApprovePO"] as List<string>;
            supplierId = searchInfo[0];
            List<PurchaseOrderListDisplay> list = control.GetPurchaseOrderBySupplierId(supplierId);
            if (list.Count <= 0)
            {
                lblMessage.Text = "There is no purchase order requested by " + ddlRequestedBy.SelectedItem.Text + " for " + ddlSupplierName.SelectedItem.Text;
            }
            gvPurchaseOrderList.DataSource = list;
            gvPurchaseOrderList.DataBind();
        }

        private void GenerateGridViewByRequestedBy()
        {
            lblMessage.Text = "";
            List<string> searchInfo = ViewState["ApprovePO"] as List<string>;
            requestedByString = searchInfo[2];
            requestedBy = Convert.ToInt32(requestedByString);
            List<PurchaseOrderListDisplay> list = control.GetPurchaseOrderByRequestedBy(requestedBy);
            if (list.Count <= 0)
            {
                lblMessage.Text = "There is no purchase order requested by " + ddlRequestedBy.SelectedItem.Text + " for " + ddlSupplierName.SelectedItem.Text;
            }
            gvPurchaseOrderList.DataSource = list;
            gvPurchaseOrderList.DataBind();
        }

        private void GenerateGridViewBySupplierIdAndRequestedBy()
        {
            lblMessage.Text = "";

            List<string> searchInfo = ViewState["ApprovePO"] as List<string>;
            supplierId = searchInfo[0];
            requestedByString = searchInfo[2];
            requestedBy = Convert.ToInt32(requestedByString);
            List<PurchaseOrderListDisplay> list = control.GetPurchaseOrderBySupplierIdAndRequestedBy(supplierId, requestedBy);
            if (list.Count <= 0)
            {
                lblMessage.Text = "There is no purchase order requested by " + ddlRequestedBy.SelectedItem.Text + " for " + ddlSupplierName.SelectedItem.Text;
            }
            gvPurchaseOrderList.DataSource = list;
            gvPurchaseOrderList.DataBind();
        }
        #endregion

        #region GridView Function
        protected void gvPurchaseOrderList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvPurchaseOrderList.PageIndex = e.NewPageIndex;
                gvPurchaseOrderList.EditIndex = -1;
                gvPurchaseOrderList.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
            }
        }

        protected void gvPurchaseOrderList_PageIndexChanged(object sender, EventArgs e)
        {
            try
            {
                List<string> searchInfo = ViewState["ApprovePO"] as List<string>;
                supplierId = searchInfo[0];
                supplierName = searchInfo[1];
                requestedByString = searchInfo[2];
                requestedBy = Convert.ToInt32(requestedByString);

                if (supplierId.Equals("ALL"))
                {
                    if (requestedBy == 0)
                    {
                        GenerateGridView();
                    }
                    else
                    {
                        GenerateGridViewByRequestedBy();
                    }
                }
                else
                {
                    if (requestedBy == 0)
                    {
                        GenerateGridViewBySupplierId();
                    }
                    else
                    {
                        GenerateGridViewBySupplierIdAndRequestedBy();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion

    }
}